﻿using System;
using System.Collections.Generic;
using DrawableObjects.Actors.Aircrafts;
using InterfaceComponents;
using WorldView;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using GlobalGameClasses;
using DrawableObjects.Actors.Aircrafts.Controller;
using Sirian.DrawableObject.Actors;
using System.Diagnostics;
using DrawableObjects.Actors;

namespace Serialization.Script
{
    /// <summary>
    /// An event that is thrown as a boss is created.
    /// </summary>
    public class Event_CreateBoss : ScriptedEvent
    {
        private Aircraft bossAircraft
        {
            get;
            set;
        }

        /// <summary>
        /// Passes specific agruments to the boss, creates HUDComponents to display the bosses statistics and finally adds the boss
        /// to the world.
        /// </summary>
        /// <param name="world">The world to insert the boss into.</param>
        /// <param name="visibleArea">Boundings declaring the visible area.</param>
        /// <param name="useableArea">Boundings declaring the usable area to insert the boss into.</param>
        public virtual void execute(World world, Bounding visibleArea, Bounding useableArea)
        {
            Debug.Assert(visibleArea.upperLeft.X > useableArea.upperLeft.X);
            Debug.Assert(visibleArea.upperLeft.Y > useableArea.upperLeft.Y);
            Debug.Assert(visibleArea.upperLeft.X + visibleArea.width < useableArea.upperLeft.X + useableArea.width);
            Debug.Assert(visibleArea.upperLeft.Y + visibleArea.height < useableArea.upperLeft.Y + useableArea.height);

            bossAircraft.aircraftAttributes.Add(Aircraft.AircraftAttributes.Triggers_Victory);
            Texture2D health;
            if (!GlobalGameValues.Values.content.Textures.TryGetValue("health", out health))
            {
                throw new System.ArgumentException("Couldn't find health texture");
            }
            Healthbar healthBar = new Healthbar(new Rectangle(GlobalGameValues.Values.graphicsDevice.PresentationParameters.BackBufferWidth / 2 - 200 - 250/2, 50, 400, 30), 
                health, 100, new Vector2(GlobalGameValues.Values.graphicsDevice.PresentationParameters.BackBufferWidth / 2 - 200 - 250/2, 50), GlobalGameClasses.GlobalGameValues.Values.spriteBatch, bossAircraft.maxHealth);
            world.addHUDComponent(healthBar);
            bossAircraft.addHealthBar(healthBar);
            bossAircraft.actorAttributes.Add(Actor.ActorAttributes.Floating);
            bossAircraft.aircraftAttributes.Add(Aircraft.AircraftAttributes.Triggers_Victory);
            bossAircraft.aircraftAttributes.Add(Aircraft.AircraftAttributes.Indestructible_Collissions);

            AIControlledAircraft_Boss controller = new AIControlledAircraft_Boss(bossAircraft);
            controller.directionMiddle = new Vector2(visibleArea.upperLeft.X - useableArea.upperLeft.X + visibleArea.width / 2, visibleArea.upperLeft.Y - useableArea.upperLeft.Y + visibleArea.height / 3);
            controller.directionLeft = new Vector2(visibleArea.upperLeft.X - useableArea.upperLeft.X + visibleArea.width / 2 - 100, visibleArea.upperLeft.Y - useableArea.upperLeft.Y + visibleArea.height / 3);
            controller.directionRight = new Vector2(visibleArea.upperLeft.X - useableArea.upperLeft.X + visibleArea.width / 2 + 100, visibleArea.upperLeft.Y - useableArea.upperLeft.Y + visibleArea.height/ 3);

            bossAircraft.controller = controller;
            world.addActor(bossAircraft);

            Vector2 insertPoint = new Vector2(visibleArea.upperLeft.X - useableArea.upperLeft.X + visibleArea.width / 2, useableArea.upperLeft.Y + bossAircraft.boundingSphere.Radius * 2); ;
            bossAircraft.setPosition(insertPoint);
            bossAircraft.standardRotation = Matrix.CreateRotationY((float)Math.PI);

        }

        public Event_CreateBoss(Aircraft boss, int eventTime)
        {
            this.bossAircraft = boss;
            this.eventTime = eventTime;
        }

        public override int getNumberOfEnemies()
        {
            return 1;
        }

    }
}

